package work.fram;

import com.google.common.base.Joiner;
import work.config.Constant;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class AutoLogger {


    private Map<String, FileWriter> map = new HashMap<>();

    private static final AutoLogger INTERFACE = new AutoLogger();

    public static AutoLogger getINTERFACE() {
        return INTERFACE;
    }

    private static final File logFile = new File(Constant.work, "log");

    static{
        if (!logFile.exists()) {
            logFile.mkdirs();
        }
    }

    public void writeLine(String file, String... msg) {

        FileWriter writer = map.get(file);
        if (writer == null) {
            try {

                writer = new FileWriter(new File(logFile, file + ".log"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            map.put(file, writer);
        }

        try {
            writer.write(Joiner.on("\t").join(msg) + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void close() {
        for (FileWriter value : map.values()) {
            try {
                value.flush();
                value.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void flush() {
        for (FileWriter value : map.values()) {
            try {
                value.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
